package com.yuwei.controller;

import com.alibaba.fastjson.JSON;
import com.yuwei.bean.Meta;
import com.yuwei.bean.User;
import com.yuwei.bean.UserData;
import com.yuwei.bean.Users;
import com.yuwei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/users")
    public String getUsers(@RequestParam String query, @RequestParam int pagenum, @RequestParam int pagesize) {
        Users users = userService.getUsers(query, pagenum, pagesize);
        return JSON.toJSONString(users);
    }

    @PostMapping("/adduser")
    public String addUser(@RequestBody User user) {
        Meta meta = new Meta();
        User retUser = userService.addUser(user);
        if (retUser != null) {
            meta.setStatus(201);
            meta.setMsg("添加用户成功");
        } else {
            meta.setStatus(0);
            meta.setMsg("用户" + user.getUserName() + "已经存在");
        }
        UserData userData = new UserData();
        userData.setMeta(meta);
        userData.setUser(retUser);
        return JSON.toJSONString(userData);
    }


    @PutMapping("users/{id}/{state}")
    public String updateUser(@PathVariable int id, @PathVariable Boolean state) {
        User retUser = userService.update(id, state);
        Meta meta = new Meta();
        if (retUser == null) {
            meta.setStatus(0);
            meta.setMsg("设置状态失败");
        } else {
            meta.setStatus(200);
            meta.setMsg("设置状态成功");
        }

        UserData userData = new UserData();
        userData.setMeta(meta);
        userData.setUser(retUser);
        return JSON.toJSONString(userData);
    }

    @GetMapping("/users/{id}")
    String getUserByID(@PathVariable int id){

        Meta meta1 = new Meta();
        User user = userService.getUserByID(id);
        if (user != null) {
            meta1.setStatus(200);
            meta1.setMsg("查询成功");
        } else {
            meta1.setStatus(0);
            meta1.setMsg("查询失败");
        }
        Object retData = new Object() {
            public Object data = new Object() {
                public int id = user.getId();
                public String username = user.getUserName();
                public int role_id = user.getRole_id();
                public String mobile = user.getMobile();
                public String email = user.getEmail();
            };
            public Meta meta = meta1;
        };
        return JSON.toJSONString(retData);
    }

    @PutMapping("/users/{id}")
    String updateUser(@PathVariable int id,@RequestBody User user){
        Meta meta1 = new Meta();
        User retUser = userService.update(id, user);
        if(retUser!=null){
            meta1.setStatus(200);
            meta1.setMsg("更新"+retUser.getUserName()+"用户成功");
        }else{
            meta1.setStatus(0);
            meta1.setMsg("更新"+retUser.getUserName()+"用户失败");
        }
        Object retData = new Object() {
            public Meta meta = meta1;
            public Object data  =new Object(){
                public String mobile = retUser.getMobile();
                public String email = retUser.getEmail();
                public int id = retUser.getId();
                public int role_id = retUser.getRole_id();
            };
        };
        return JSON.toJSONString(retData);
    }

    @DeleteMapping("/users/{id}")
    public String delUserByID(@PathVariable int id){
        boolean bRet = userService.delUserByID(id);
        Object retData = new Object() {
            public Meta meta = new Meta("删除成功",200);
            public Object data = null;
        };
        return JSON.toJSONString(retData);
    }
}
